#include "led_control.h"
#include "smart_pwm.h"



/*
 * 函数介绍: 1.OBU发出车辆解锁信号，依次显示白色和蓝色的欢迎动画，由内向两侧示阔，进行两次示阔。
 * 参数:
 * 返回值: {*}
 * 备注:
 */
void led_Vehicle_Unlock_func(void)
{
    static uint32_t w1MSTick=0;
    uint8_t  Number_of_cycles = 0;
    if ( (UNLOCK_OR_LOCK_TIME <= Get1MsTickInterval(w1MSTick)) )
    {
        w1MSTick = Get1MsTickVal();
        Number_of_cycles = Vehicle_Unlock_func_set_smart_pwm();
        send_smart();
        if (2 == Number_of_cycles)
        {
           st_Light_effect_now(LIGHT_EFFECT_SIGNAL_MAX);
           close_smart_pwm();
           send_smart();
        } 
    }else{}
}

/*
 * 函数介绍: 2.OBU发出车辆上锁信号，显示黄色再见动画，由两侧外向内收缩，循环2次。（黄色）→→→
 * 参数:
 * 返回值: {*}
 * 备注:
 */
void led_Vehicle_lock_func(void)
{
    static uint32_t w1MSTick=0;
    uint8_t  Number_of_cycles = 0;
    if ( (UNLOCK_OR_LOCK_TIME <= Get1MsTickInterval(w1MSTick)) )
    {
        w1MSTick = Get1MsTickVal();
        Number_of_cycles = Vehicle_lock_func_set_smart_pwm();
        send_smart();
        if (2 == Number_of_cycles)
        {
           st_Light_effect_now(LIGHT_EFFECT_SIGNAL_MAX);
           close_smart_pwm();
           send_smart();
        } 
    }else{}
}

/*
 * 函数介绍: OBU发出车辆转向信号，左转：向左流动，循环10次。（蓝色）→→→
 * 参数:
 * 返回值: {*}
 * 备注:
 */
void led_Vehicle_turning_left_func(void)
{
    static uint32_t w1MSTick=0;
    static uint8_t clear_flag = 0;
    uint8_t  Number_of_cycles = 0;
    if(0 == clear_flag)/*第一次进来清除灯的PWM 如果被模式打断可能出现bug*/
    {
    	close_smart_pwm();
    	clear_flag = 1;
    }else{}
    if ( (LEFT_AND_RIGHT_TIME <= Get1MsTickInterval(w1MSTick)) )
    {
       w1MSTick = Get1MsTickVal();
       Number_of_cycles = Vehicle_turning_left_func_set_smart_pwm();
       send_smart();
       if (10 == Number_of_cycles)
       {
           st_Light_effect_now(LIGHT_EFFECT_SIGNAL_MAX);
    	   clear_flag = 0;
           close_smart_pwm();
           send_smart();
       } 
    }else{}
}

/*
 * 函数介绍: OBU发出车辆转向信号，右转：向右流动，循环10次。（蓝色）→→→
 * 参数:
 * 返回值: {*}
 * 备注:
 */
void led_Vehicle_turning_right_func(void)
{
    static uint32_t w1MSTick=0;
    static uint8_t clear_flag = 0;
    uint8_t  Number_of_cycles = 0;
    if(0 == clear_flag)/*第一次进来清除灯的PWM 如果被模式打断可能出现bug*/
    {
    	close_smart_pwm();
    	clear_flag = 1;
    }else{}
    if ( (LEFT_AND_RIGHT_TIME <= Get1MsTickInterval(w1MSTick)) )
    {
       w1MSTick = Get1MsTickVal();
       Number_of_cycles = Vehicle_turning_right_func_set_smart_pwm();
       send_smart();
       if (10 == Number_of_cycles)
       {
           st_Light_effect_now(LIGHT_EFFECT_SIGNAL_MAX);
    	   clear_flag = 0;
           Number_of_cycles = 0;
           close_smart_pwm();
           send_smart();
       } 
    }
}

/*
 * 函数介绍: OBU发出预警信号主逻辑函数
 * 参数:
 * 返回值: {*}
 * 备注:
 */
void led_Vehicle_warning_func(void)
{
    static uint32_t w1MSTick=0;
    static uint8_t  Mode_switching = 0;
    if ( (HUMAN_EYE_21ms <= Get1MsTickInterval(w1MSTick)) )
    {
       w1MSTick = Get1MsTickVal();
        if (0 == Mode_switching)
        {
            Mode_switching++;
            Vehicle_warning_func_set_smart_pwm();
        }else{
            Mode_switching = 0;
            close_smart_pwm();
        }
        send_smart();
    }
}

/*
 * 函数介绍: OBU发出解除预警主逻辑函数
 * 参数:
 * 返回值: {*}
 * 备注:
 */
void led_Vehicle_release_warning_func(void)
{
    close_smart_pwm();
    send_smart();
}

/*
 * 函数介绍: OBU发出车辆充电主逻辑函数
 * 参数:
 * 返回值: {*}
 * 备注:
 */
void led_Vehicle_charging_func(void)
{
    static uint32_t w1MSTick=0;
    if ( (CHARGING_TIME <= Get1MsTickInterval(w1MSTick)) )
    {
       w1MSTick = Get1MsTickVal();
       Vehicle_charging_func_set_smart_pwm();
       send_smart();
    }
}

/*
 * 函数介绍: LED控制逻辑主函数
 * 参数:
 * 返回值: {*}
 * 备注:
 */
void led_control_main()
{
    /*判断是否有功能需要执行*/
    switch (rt_Light_effect_now())
    {
        case VEHICLE_UNLOCKING:         /*OBU发出车辆解锁信号*/
            led_Vehicle_Unlock_func();
            break;
        case VEHICLE_LOCKING:           /*OBU发出车辆上锁信号*/
            led_Vehicle_lock_func();
            break;
        case VEHICLE_TURNING_LEFT:      /*OBU发出车辆左转信号*/
            led_Vehicle_turning_left_func();
            break;
        case VEHICLE_TURNING_RIGHT:     /*OBU发出车辆右转信号*/
            led_Vehicle_turning_right_func();
            break;
        case VEHICLE_WARNING:           /*OBU发出预警信号*/
            led_Vehicle_warning_func();
            break;
        case VEHICLE_RELEASE_WARNING:   /*OBU发出解除预警信号*/
            led_Vehicle_release_warning_func();
            break;
        case VEHICLE_CHARGING:          /*OBU发出充电信号*/
            led_Vehicle_charging_func();
            break;
        case LIGHT_EFFECT_SIGNAL_MAX:   /*当前无信号*/
            /* code */
            close_smart_pwm();
            send_smart();
        	break;
        default:
            close_smart_pwm();
            send_smart();
            break;  
    }
}
